package cn.afactory.tygz.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.controller.MessageController;
import cn.afactory.tygz.model.SysMessageItem;
import cn.afactory.tygz.model.SysMessageItemList;
import cn.afactory.tygz.ui.activities.SysMessageDetailActivity;
import cn.afactory.tygz.ui.adapter.SysMessageAdapter;
import cn.afactory.tygz.ui.base.BaseActivity;
import cn.afactory.tygz.ui.base.thirdpart.pulltorefresh.PullToRefreshBase;
import cn.afactory.tygz.ui.base.thirdpart.pulltorefresh.PullToRefreshListView;
import cn.afactory.tygz.ui.base.utils.LocalUtils;

/**
 * Created with Android Studio.
 * Title:SysMessageListActivity
 * Description:
 * Copyright:Copyright (c) 2015
 * Company:和平必胜、正义必胜、人民必胜
 * Author:炎黄子孙
 * Date:15/9/21
 * Time:下午8:54
 * Version 1.0
 */
@ContentView(R.layout.ac_sys_message_list)
public class SysMessageListActivity extends BaseActivity{
    @ViewInject(R.id.plv_subject)
    private PullToRefreshListView pSysListView;

    private SysMessageAdapter mAdapter;
    private List<SysMessageItem> mList = new ArrayList<>();
    private MessageController mController;

    protected int mPageIndex = 1;
    protected int mPageSize = 10;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);

        mController = new MessageController(this,mUIHandler);
        initDatas();
        initTitleBar();
    }

    private void initDatas(){
        mAdapter = new SysMessageAdapter(this,mList);
        pSysListView.setAdapter(mAdapter);
        pSysListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    Intent intent =new Intent(mContext, SysMessageDetailActivity.class);
                    intent.putExtra("msg",mList.get(position-1));
                startActivity(intent);
            }
        });

        mController.doGetSysMsgList(mPageIndex,mPageSize,System.currentTimeMillis());
    }
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what){
            case MessageController.MSG_LOAD_SYS_MSG_LIST_OK:
                handleMessageList((SysMessageItemList) msg.obj);
                break;
            case MessageController.MSG_LOAD_SYS_MSG_LIST_KO:
                if (mAdapter.getCount() == 0) {
                    hideErrorPage(-1);
                    showErrorPage(msg.arg1, new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            startLoadData(true);
                        }
                    });
                } else {
                    LocalUtils.showToast(mContext, (String) msg.obj);
                }
                break;
        }
        // 结束下拉刷新
        if (pSysListView.isRefreshing()) {
            pSysListView.onRefreshComplete();
        }
    }

    private void initTitleBar(){
        setTitleText(getString(R.string.label_title_sys_message));
    }

    /**
     * 跳转到系统消息列表页面
     * @param context
     */
    public static void gotoSysMessageList(Context context){
        context.startActivity(new Intent(context,SysMessageListActivity.class));
    }

    /**
     * 处理系统信息列表
     * @param t
     */
    private void handleMessageList(SysMessageItemList t){
        hideErrorPage(-1);
        if (t.value != null && t.value.size() >= 0) {
            try {
                mList.addAll(t.value);
                //是否显示置顶链接VIEW
                mAdapter.setData(mList);
                mPageIndex = (mList.size()/mPageSize) + 1;
                setListListener();

                if (pSysListView.isRefreshing()) {
                    pSysListView.onRefreshComplete();
                }
                if (t.value.size() < mPageSize) {
                    pSysListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                } else {
                    pSysListView.setMode(PullToRefreshBase.Mode.BOTH);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            if (mAdapter.getCount() == 0) {
                showNullPage(true);
            }
        }
    }

    /***
     * 设置监听器
     */
    private void setListListener(){
        pSysListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                startLoadData(true);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                startLoadData(false);
            }
        });
    }

    private void startLoadData(boolean cleanData){
        if(cleanData){
            mList.clear();
            mPageIndex = 1;
        }

        mController.doGetSysMsgList(mPageIndex,mPageSize,System.currentTimeMillis());
    }
}
